<?php
/**
 * Created by PhpStorm.
 * User: lwt
 * Date: 4/25/19
 * Time: 8:31 PM 
 */

namespace app\admin\controller;

use think\Request;
use app\admin\model\CodeModel;
use app\admin\model\CommodityModel;
use app\admin\model\BatchModel;

class Code
{
    public function addBatchParentCode(){
        $request = Request::instance();
        $isToken = isToken($request->get(),$request->get("token"));
        if ($isToken["code"] != 200){
            return json($isToken);
        }
        $content = tokenVerification($request->get("token"));
        if($content["code"] != 200){
            return json($content);
        }
        $data = $request->post();
        $batchMsg = BatchModel::selectBatchByCoId($data["co_id"]);
        $commodityMsg = CommodityModel::selectCommodityById($batchMsg[0]["cos_id"]);
        $capacity = $commodityMsg[0]["co_capacity"];
        $time = time();
        $msg = array();
        for ($x = 0; $x < $data["codeNum"];$x++){
            $msg[]  = [
                "pc_id" => "M".$time.$x,
                "co_id" => $data["co_id"],
                "pc_num" => 0
            ];
            $msgSub = array();
            for ($y = 0;$y <$capacity;$y++){
                $msgSub[] = [
                    "sc_id" => "Z".$time.$x."_".$y,
                    "pc_id" => "M".$time.$x,
                    "sc_num" => 0
                ];
            }
            CodeModel::addBatchSubCode($msgSub);
        }
        CodeModel::addBatchParentCode($msg);
        
        $result["code"] = 200;
        $result["msg"] = "添加成功！";
        return json($result);
    }

    public function selectCodeByCoID(){
        $request = Request::instance();
        $isToken = isToken($request->get(),$request->get("token"));
        if ($isToken["code"] != 200){
            return json($isToken);
        }
        $content = tokenVerification($request->get("token"));
        if($content["code"] != 200){
            return json($content);
        }
        $data = $request->post();
        $pcList = CodeModel::selectAllParentCode($data);
        $pcUnm = count($pcList);
        for ($x = 0; $x < $pcUnm;$x++){
            $pcList[$x]["subCodeList"] = CodeModel::selectAllSubCode($pcList[$x]["pc_id"]);
        }
        $result["code"] = 200;
        $result["num"] = $pcUnm;
        $result["msg"] = $pcList;
        return json($result);
    }

    public function selectCodeById(){
        $request = Request::instance();
        $isToken = isToken($request->get(),$request->get("token"));
        if ($isToken["code"] != 200){
            return json($isToken);
        }
        $content = tokenVerification($request->get("token"));
        if($content["code"] != 200){
            return json($content);
        }
        $data = $request->post();
        $pcList = CodeModel::selectAllParentCode($data["co_id"]);
        $pcUnm = count($pcList);
        for ($x = 0; $x < $pcUnm;$x++){
            $pcList[$x]["subCodeList"] = CodeModel::selectAllSubCode($pcList[$x]["pc_id"]);
        }
        $result["code"] = 200;
        $result["num"] = $pcUnm;
        $result["msg"] = $pcList;
        return json($result);
    }

    public function selectCommodityById(){
        $request = Request::instance();
        $isToken = isToken($request->get(),$request->get("token"));
        if ($isToken["code"] != 200){
            return json($isToken);
        }
        $content = tokenVerification($request->get("token"));
        if($content["code"] != 200){
            return json($content);
        }
        $data = $request->post();
        $msg = CodeModel::selectCommodityById($data["pc_id"]);
        $result["code"] = 200;
        $result["msg"] = $msg;
        return json($result);
    }
}